package com.rwb.springboot.sell.controller;

import com.rwb.springboot.sell.cenverter.OrderForm2OrderDTOConverter;
import com.rwb.springboot.sell.dto.OrderDTO;
import com.rwb.springboot.sell.enums.ResultEnum;
import com.rwb.springboot.sell.exception.MallException;
import com.rwb.springboot.sell.form.OrderForm;
import com.rwb.springboot.sell.service.BuyerService;
import com.rwb.springboot.sell.service.OrderService;
import com.rwb.springboot.sell.utils.ResultVOUtil;
import com.rwb.springboot.sell.vo.ResultVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by ruwenbo on 2018/1/2.
 */
@RestController
@RequestMapping("/buyer/order")
public class BuyerOrderController {

    private static final Logger LOGGER = LoggerFactory.getLogger(BuyerOrderController.class);

    @Autowired
    private OrderService orderService;

    @Autowired
    private BuyerService buyerService;



    // 创建订单
    @PostMapping("/create")
    public ResultVO<Map<String,String>> create(@Valid OrderForm orderForm,
                                               BindingResult bindingResult) {
        Map<String, String> resultMap = new HashMap<String, String>();
        // 校验表单验证之后有没有错误
        if (bindingResult.hasErrors()) {
            LOGGER.error("【创建订单】参数不正确，orderForm={}", orderForm);
            throw new MallException(ResultEnum.PARAM_ERROR.getCode(),
                    bindingResult.getFieldError().getDefaultMessage());
        }
        OrderDTO orderDTO = OrderForm2OrderDTOConverter.converter(orderForm);
        if (CollectionUtils.isEmpty(orderDTO.getOrderDetailList())) {
            LOGGER.error("【创建订单】购物车不能为空");
            throw new MallException(ResultEnum.CART_EMPTY);
        }

        OrderDTO createResult = orderService.create(orderDTO);
        resultMap.put("orderId", createResult.getOrderId());

        return ResultVOUtil.success(resultMap);
    }

    // 订单列表
    @GetMapping("/list")
    public ResultVO<List<OrderDTO>> list(@RequestParam("openid") String openid,
                                         @RequestParam(value = "page",defaultValue = "0") Integer page,
                                         @RequestParam(value = "size", defaultValue = "10") Integer size) {
        if (StringUtils.isEmpty(openid)) {
            LOGGER.error("【查询订单列表】openid为空");
            throw new MallException(ResultEnum.PARAM_EMPTY);
        }
        PageRequest request = new PageRequest(page, size);
        Page<OrderDTO> orderDTOS = orderService.findList(openid, request);
        return ResultVOUtil.success(orderDTOS.getContent());
    }

    // 订单详情
    @GetMapping("/detail")
    public ResultVO<OrderDTO> detail(@RequestParam("openid") String openid,
                                     @RequestParam("orderId") String orderId) {
        OrderDTO orderDTO = buyerService.findOrderOne(openid, orderId);
        return  ResultVOUtil.success(orderDTO);
    }


    // 取消订单
    @PostMapping("/cancel")
    public ResultVO cancel(@RequestParam("openid") String openid,
                           @RequestParam("orderId") String orderId) {
        buyerService.cancel(openid, orderId);
        return  ResultVOUtil.success();
    }

}
